SHELL := /bin/bash
TOOLCHAIN_DIR := $(shell echo $${TOOLCHAIN_DIR:-/usr/local/k1tools/})
# Compilers
K1CC            ?= $(TOOLCHAIN_DIR)/bin/k1-gcc
CREATE_MULTIBIN ?= $(TOOLCHAIN_DIR)/bin/k1-create-multibinary

CLUSTER_FILES := cluster.c Functions.c
IO_FILES := test_io.c 
IO_APP_NAME := ioApp
CLUSTER_APP_NAME := clusterApp
MULTIBIN_NAME := multibinApp.mpk

all:    mppa

mppa: clean
	${K1CC} -g -mos=nodeos ${CLUSTER_FILES} -o ${CLUSTER_APP_NAME} -lmppaipc
	${K1CC} -g -mos=rtems test_io.c -o ${IO_APP_NAME} -lmppaipc
	${CREATE_MULTIBIN} --toolchain ${TOOLCHAIN_DIR} --clusters ${CLUSTER_APP_NAME} --boot=${IO_APP_NAME} -T ${MULTIBIN_NAME}

traces: clean
	${K1CC} -g -DMPPA_TRACE_ENABLE -mos=nodeos ${CLUSTER_FILES} -o ${CLUSTER_APP_NAME} -lmppaipc
	${K1CC} -g -DMPPA_TRACE_ENABLE -mos=rtems ${IO_FILES} -o ${IO_APP_NAME} -lmppaipc
	${CREATE_MULTIBIN} --toolchain ${TOOLCHAIN_DIR} --clusters ${CLUSTER_APP_NAME} --boot=${IO_APP_NAME} -T ${MULTIBIN_NAME}
	
run-sim-traces:
	${TOOLCHAIN_DIR}/bin/k1-mppa -p -- ./${MULTIBIN_NAME}

.PHONY: run-mppa
run-mppa:
	${TOOLCHAIN_DIR}/bin/k1-jtag-runner --multibinary ${MULTIBIN_NAME} --exec-multibin=IODDR0:${IO_APP_NAME}
	
.PHONY: clean
clean:
	rm -rf ${IO_APP_NAME} ${CLUSTER_APP_NAME} ${MULTIBIN_NAME}
